/*
 * @Author: OreoWang
 * @Email: ihc523@163.com
 * @Date: 2020-06-01 12:05:23
 * @LastEditors: OreoWang
 * @LastEditTime: 2022-08-10 17:19:50
 * @Description: 模型视图控制器
 */
import { _decorator, Component, Node, EventTouch } from "cc";
const { ccclass, property } = _decorator;

import ExampleModel, { ModelData } from "./ExampleModel";
import ExampleView from "./ExampleView";
import ViewController from "../core/ViewController";
import { logger } from "../core/ViewLogger";

@ccclass("ExampleController")
export default class ExampleController extends ViewController<ExampleView, ExampleModel, ModelData> {      
    __preload(){
        this.data = new ModelData();
        this.model = new ExampleModel(this, this.view, this.data);
    }

    start(){
        let testData = false;
        // 指定 data 的值，会更新到 view
        if(testData){
            this.data.strName = "ModelName",
            this.data.strPwd = "ModelPassword",
            this.data.isRemember = true;         
        }
        // 指定 view 的值，会更新到 data
        else{
            this.view.editName.string = "ViewName";
            this.view.editPassword.string = "ViewPassword";
            this.view.togRemember.isChecked = true;
        }

        // 多级属性
        this.data.json.k1 = "value1";

        //事件监听
        this._listen(this.view.btnLogin, "", this.onBtnLogin);

        //以下两种写法等价，默认回调函数为 onEventHandler
        this._listen(this.view.btnReg, "data");
        // this._listen(this.view.btnReg, "data", this.onEventHandler);
    }

    onValueChange(key: string, value: any, oldValue: any){
        logger.log("onValueChange: ", key, value, oldValue);

        let json = JSON.stringify(this.data, null, 4);
        logger.warn("data: ", json);
    }

    onBtnLogin(event: EventTouch, data: string){
        logger.log("点击登录：", event, data);
    }
}
